<template>
  <div v-if="!item.hidden">
    <template
      v-if="
        hasOneShowingChild(item.children, item) &&
        (!state.onlyOneChild.children ||
          state.onlyOneChild.noShowingChildren) &&
        !item.alwaysShow
      "
    >
      <AppLink
        v-if="state.onlyOneChild.meta"
        :to="resolvePath(state.onlyOneChild.path, state.onlyOneChild.query)"
      >
        <el-menu-item :index="resolvePath(state.onlyOneChild.path)">
          <item
            :icon="
              state.onlyOneChild.meta.icon || (item.meta && item.meta.icon)
            "
            :title="state.onlyOneChild.meta.title"
          />
        </el-menu-item>
      </AppLink>
    </template>
    <el-sub-menu :index="resolvePath(state.onlyOneChild.path)" v-else>
      <template #title v-if="item.meta">
        <item
          v-if="item.meta"
          :icon="item.meta && item.meta.icon"
          :title="item.meta.title"
        />
      </template>
      <sidebar-item
        v-for="child in item.children"
        :key="child.path"
        :item="child"
        :is-nest="true"
        :base-path="resolvePath(child.path)"
      />
    </el-sub-menu>
  </div>
</template>
<script setup lang="ts">
import { ref, reactive, defineEmits, onMounted, defineProps } from "vue";
import { isExternal } from "/@ts/plugins/validate";
import path from "path-browserify";
import Item from "/@/layouts/components/Sidebar/Item.vue";
import AppLink from "/@/layouts/components/Sidebar/Link.vue";
import SidebarItem from "/@/layouts/components/Sidebar/SidebarItem.vue";
/*
基本数据类型
引用数据类型（复杂类型） 个人建议 ref初始化变量 
ref 和 reactive 本质我们可以简单的理解为ref是对reactive的二次包装, 
ref定义的数据访问的时候要多一个.value
*/

const state: any = reactive({
  onlyOneChild: null,
});

const props = defineProps({
  item: {
    type: Object,
    required: true,
  },
  isNest: {
    type: Boolean,
    default: false,
  },
  basePath: {
    type: String,
    default: "",
  },
});
const hasOneShowingChild = (children: Array<any> = [], parent: any) => {
  if (!children) {
    children = [];
  }
  const showingChildren = children.filter((item) => {
    if (item.hidden) {
      return false;
    } else {
      // Temp set(will be used if only has one showing child)
      state.onlyOneChild = item;
      return true;
    }
  });

  // When there is only one child router, the child router is displayed by default
  if (showingChildren.length === 1) {
    return true;
  }

  // Show parent if there are no child router to display
  if (showingChildren.length === 0) {
    state.onlyOneChild = { ...parent, path: "", noShowingChildren: true };
    return true;
  }

  return false;
};
const resolvePath = (routePath: any, routeQuery: any) => {
  if (isExternal(routePath)) {
    return routePath;
  }
  if (isExternal(props.basePath)) {
    return props.basePath;
  }
  if (routeQuery) {
    let query = JSON.parse(JSON.stringify(routeQuery));
    return { path: path.resolve(props.basePath, routePath), query: query };
  }
  return path.resolve(props.basePath, routePath);
};
</script>

<style scoped lang="scss"></style>
